﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 授权工具
    /// </summary>
    public class SoftAuthorizationUtil
    {
        /// <summary>
        /// 生产硬件加密信息
        /// </summary>
        /// <returns></returns>
        public static string GenerateEncryptHardwareInfo()
        {
            return SafetyUtil.DesEncrypt(SoftAuthorizationUtil.GetHardwareInfo(false).First<string>(), _Key, _Iv);
        }

        /// <summary>
        /// 获取所在设备硬件信息
        /// </summary>
        /// <returns></returns>
        private static List<string> GetHardwareInfo(bool isCompare)
        {
            return MachineHardwareInfoUtil.GetHardwareInfo(isCompare);
        }

        // 
        private static string AddKeyContent(string hardwareInfo)
        {
            return Key + hardwareInfo;
        }

        // 
        private static string AddLicense(string content, LicenseInfo license)
        {
            return content = content + "|License=" + JsonHelper.JsonSerialize(license);
        }

        // 生成加密密钥
        public static string GenerateFingerprint(string encryptHardwareInfo, LicenseInfo license = null)
        {
            return SafetyUtil.DesEncrypt(SoftAuthorizationUtil.AddLicense(SoftAuthorizationUtil.AddKeyContent(SafetyUtil.DesDecrypt(encryptHardwareInfo, _Key, _Iv)), license), _Key, _Iv);
        }

        // 解密
        public static string DesDecrypt(string content)
        {
            return SafetyUtil.DesDecrypt(content, _Key, _Iv);
        }

        // 对比密钥并获取激活信息
        public static bool CompareAndGetTime(string authorizationContentEncrypted, out LicenseInfo license)
        {
            license = null;
            string text = SafetyUtil.DesDecrypt(authorizationContentEncrypted, _Key, _Iv);
            foreach (string hardwareInfo in SoftAuthorizationUtil.GetHardwareInfo(true))
            {
                string text2 = SoftAuthorizationUtil.AddKeyContent(hardwareInfo);
                if (text.StartsWith(text2, StringComparison.OrdinalIgnoreCase))
                {
                    if (text.Length > text2.Length + 1)
                    {
                        string text3 = text.Substring(text2.Length + 1);
                        if (text3.StartsWith("License"))
                        {
                            license = JsonHelper.JsonDeserialize<LicenseInfo>(text3.Substring("License".Length + 1));
                        }
                    }
                    return true;
                }
            }
            return false;
        }

        private const string _Key = "fGhjsK78";
        private const string _Iv = "ss8edKe3rTsfhH43c+ddwKUs";
        private const string Key = "03F55AFA-AF6B-4AF9-A532-858375D78448_";

        public static readonly string HardwareInfoExtension = "mhi";
        public static readonly string FingerprintCertificateExtension = "mfp";
        public static readonly string FingerprintName = "Fingerprint";
        public static readonly string AuthorizationDir = "Authorization";
    }
}
